Fixed issue#5, continued to fix other problems