Refactored codes, added more tests