more refactoring and bug fixes.
added TODOs