basic working implementation, but not optimized yet