working version with a few test cases, still need to condense the code and remove some dependencies