WORKING CODE, COMMENTS PENDING