Fixed security rules. Cleaned up code and updated comments.