Working version, lacks lot of comments. Property names are subject to change.