initial commit of port; already working; needs more work to make more elegant and readable; needs more tests