Subroutines refactored to methods. Exception handling added (returning zero on NaN input).