refactored big parts of code