improved, but still needs to be debugged