Simplification of the code

Instead of 3 if statements containing almost the same code, one switch statement and one small helper method.