Revisted code and fixed some problems.