working version, still needs some tweaking