bug fixes, code refactor