Made some ammendments and merged Mark's code

Change-Id: Idc50503e8b9bef65933c04fbbb9902406e44e27c