Attempt to optimize some parts of the code by using switches & unwrapped loops