Updated version with better architecture-however far from finished.