Can I get inline comments explaining this algorithm? Also can you update the javadoc to explain when it returns null?