refactored quite some code