fixed bugs and make it work