refactor most of the code to work with typescript