this code is mostly working