refactored, cut fat, cleaned up coding style, added documentation