Refactored code, more stable and efficient.