few bug fixes, and some rearranging