minor changes and some redundant codes removed