Code and documentation cleanup; fixed #317 and #464