Re-implemented most of the code for better performance

[TODO] Implement mouse interaction.