removed static context and calls, refactored messy code, added comments