removed some globals, faster code + added comments