Refactored code, removed all errors and warnings