Avoiding division by zero, minor coding style improvements