Refactor and Bugfix