litte bug fixes and some refactoring
code refactoring