various improvements, but it still needs a complete rewrite from scratch