Improve code and add commments.