Modified the code. Improves #1