Pull request to master

PS the comments have been a lot of help, even solved a memory leak that I was supposed to do for later but was fortunately gone after following (and learning from) the comments. :+1: