Fixed the bug (still need some tweaking)