pretty much working
refactored the code, now it is ready for a final review