reorganizing and minor bug fixes