First draft of source code (not tested and bugged)