Fixed context, renamed functions, added comments