some work done, but switching to JS version at the moment
S