Added memory-leak detection. Made sure to not change state after frames if not neccessary.