Merged Saman's code/features