Working version. Cleaned up