MODIFIED: cleaned up the code after all the fixes