Removed repeated code and switched if-statement to ternary.

Signed-off-by: Lucas Arnström <lucasarnstrom@gmail.com>