Reshuffle code to be a native javascript lib