refactor code, document it better