First version, not fully tested but functional enough