code style: named functions, else if; bugfix