refactored a bit, fixed some things