Added performance improvements

Removed the giant for-loops and replaced them entirely with if-then
statements