Some cleanup and memory clean (thanks @aitek4iit for noticing)