Correcting and improving code
Merge with trunk