cleaned up code, beginning to make portable