Style corrections, two ifs changed to switch cases for easier understanding/debugging, sql error corrected related to @036355297d