Code cleanup

This update modularizes some of the code into ksatools.py and adds a few more test cases.