code refactoring + bugfixing