Some progress on #36 but I introduced a new bug
Close #36