Refactored and fixed stuff