Added a few more changes to fix last bug. Still need to solve the type casting problem.