Fixed rule bug, slightly better program structure for clarity.