fixed bugs and function