Optimized space handling

Removed an un-neccesary loop and the space consuming buffer arrays.

Improved the running time to O(n)
Merge pull request #1 from Dawny33/patch-1

Optimized space handling