everything works, code refactored, code also commited for clarity