Added copyright header
Fixed a memory leak + some cleanups