Merged idleberg's excellent work!