fixing behavior, minor code refactoring