bugfixes and optimization
started working on a version in c. still buggy...