Finished rules
Fixed various bugs and added test cases