Fixed a switch bug and refactored a for statement