made some progress. Still not working perfectly...