Re-organised and added unit tests