made some fixes and refactoring